package com.maven.front.service.manageSubject;

import com.maven.front.bean.EduCourse;
import com.maven.front.bean.Result;
import com.maven.front.bean.SysSubject;
import com.maven.front.dto.ZTreeVo;
import com.maven.front.mapper.EduCourseMapper;
import com.maven.front.mapper.SysSubjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class SysSubjectService {

    @Resource
    private SysSubjectMapper sysSubjectMapper;
    @Resource
    private EduCourseMapper eduCourseMapper;

    //     查询所有专业父节点
    public List<SysSubject> select(){
        List<SysSubject> list = sysSubjectMapper.select();
        if (CollectionUtils.isEmpty(list)){
            return null;
        } else {
            return list;
        }
    }

    //    获取专业类所有数据
    public Result selectSysSubject(){
        List<SysSubject> list = sysSubjectMapper.selectSysSubject();
        List<ZTreeVo> listz = new ArrayList<ZTreeVo>();
        if (CollectionUtils.isEmpty(list)){
            return new Result(10004, "查询失败！", null);
        } else {
            for (SysSubject l:list){
                ZTreeVo zTreeVo = new ZTreeVo();
                zTreeVo.setId(l.getSubjectId());
                zTreeVo.setpId(l.getParentId());
                zTreeVo.setName(l.getSubjectName());
                listz.add(zTreeVo);
            }
            return new Result(200, "查询成功!", listz);
        }
    }

//      根据父节点获取所有子节点
    public Result getParentId(Integer subjectId){
        List<SysSubject> list = sysSubjectMapper.getPID(subjectId);
        for (SysSubject s:list){
            if (s.getParentId()==0){
                list.remove(s);
            }
        }
        if (CollectionUtils.isEmpty(list)){
            return new Result(10005, "查询失败！",null);
        } else {
            return new Result(200, "查询成功！", list);
        }
    }



    //    根据专业ID获取数据，便于修改
    public Result selectByPrimaryKey(Integer subjectId){
        SysSubject sysSubject = sysSubjectMapper.selectByPrimaryKey(subjectId);
        if (ObjectUtils.isEmpty(sysSubject)){
            return new Result(10005, "获取失败！", null);
        } else {
            return new Result(200, "获取成功！", sysSubject);
        }
    }

    //      根据前台传递过来的数据，删除节点
//            前台会判断当前节点是否为父节点，或者当前父节点下是否还有子节点，因此可以直接删除数据
    public Result deleteSubjectId(String subjectId){
        SysSubject sysSubject = new SysSubject();
        sysSubject.setSubjectId(Integer.valueOf(subjectId));
        sysSubject.setStatus(1);
//        删除节点前, 判断其专业下是否还有课程
        List<EduCourse> list = eduCourseMapper.getCourseNum(Integer.valueOf(subjectId));
        if (!CollectionUtils.isEmpty(list)){
            return new Result(10101, "当前专业下存有课程,不可删除！");
        }
        int num = sysSubjectMapper.updateByPrimaryKeySelective(sysSubject);
        if (num>0){
            return new Result(200, "删除成功！", null);
        } else {
            return new Result(10005, "删除失败！", null);
        }
    }


    //    根据获取的专业ID修改专业名称，因为是修改名称，所以不用判断其是否为父节点
    public Result updateSubject(SysSubject sysSubject){
//        获取所有专业数据
        List<SysSubject> listSubject = sysSubjectMapper.select();
        for (SysSubject sys:listSubject){
            if (sys.getSubjectName().equals(sysSubject.getSubjectName())&&sys.getSubjectId()!=sysSubject.getSubjectId()){
                return new Result(10101, "专业名称重复！", null);
            }
        }
        int num = sysSubjectMapper.updateByPrimaryKeySelective(sysSubject);
        if (num > 0){
            return new Result(200, "修改成功！");
        } else {
            return new Result(10004, "修改失败！");
        }
    }


    //     对课程类的进行章节操作
//          根据课程ID获取当前课程的所有父节点以及子节点
    public Result selectByPrimaryKey(int courseId){
        List<ZTreeVo> listz = new ArrayList<ZTreeVo>();
        EduCourse eduCourse = eduCourseMapper.selectByPrimaryKey(courseId);
        List<SysSubject> list = sysSubjectMapper.getParentId(eduCourse.getSubjectId());
        if (CollectionUtils.isEmpty(list)){
            return new Result(10004, "查询失败", null);
        } else {
            for (SysSubject l:list){
                //将节点赋值给ZTree类，生成树形图
                ZTreeVo zTreeVo = new ZTreeVo();
                zTreeVo.setId(l.getSubjectId());
                zTreeVo.setpId(l.getParentId());
                zTreeVo.setName(l.getSubjectName());
                listz.add(zTreeVo);
            }
            return new Result(200, "查询成功", list);
        }
    }


    public List<SysSubject> getFatherNode() {
        return null;
    }


//    新增章节
    public Result addSubject(Integer parentId, String subjectName){
        //        获取所有专业数据
        List<SysSubject> listSubject = sysSubjectMapper.select();
        for (SysSubject sys:listSubject){
            if (sys.getSubjectName().equals(subjectName)){
                return new Result(10101, "专业名称重复！", null);
            }
        }
        SysSubject sysSubject = new SysSubject();
        if (parentId==-1){//表示新增父节点
            sysSubject.setParentId(0);
        } else {
            sysSubject.setParentId(parentId);
        }
        sysSubject.setSubjectName(subjectName);
        sysSubject.setCreateTime(new Date());
        sysSubject.setStatus(0);
        sysSubject.setSort(0);
        int num = sysSubjectMapper.insertSelective(sysSubject);
        if (num > 0){
            return new Result(200, "新增章节成功！");
        } else {
            return new Result(10004, "新增章节失败！");
        }
    }

}
